import _ from 'lodash'
import moment from 'moment'
import {getTerminationPaymentDate} from '../api/calc/otcOption/calc'

/**
 * 返回年化系数 = 实际期限/计息基准天数
 * @param object
 */
export function calcAnnualized(daycounter, term, effectiveDate) {
    let year = effectiveDate.split('-')[0];
    var days = 365;
    if (daycounter == 'Actual/365') {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            days = 366;
        } else {
            days = 365;
        }
    } else {
        days = daycounter.replace(/[^\d]/g, '');
    }
    return _.divide(term, days);
}

/**
 * P013XEQ-2711  年化天数计算公式调整
 * @param object
 */
export function calcAnnualizedByTerminationPaymentDate(data) {
    let {effectiveDate,daycounter,terminationPaymentDate} = data
    let year = effectiveDate.split('-')[0];
    var days = 365;
    let a = moment(terminationPaymentDate)
    let b = moment(effectiveDate)
    let term = Number(a.diff(b, 'days'))
    if (daycounter == 'Actual/365') {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
            days = 366;
        } else {
            days = 365;
        }
    } else {
        days = Boolean(daycounter)? daycounter.replace(/[^\d]/g, ''): 365;
    }
    return _.divide(term, days);
}


/**
 *  判断是否为空  null empty  blank undefind NaN
 * @param object
 */
export function isVoid(param) {
    if ((_.isNull(param) || _.isEmpty(param) || _.isUndefined(param) || param == '' || "undefined" == param)
        && !Number.isFinite(param)
    ) {
        return true;
    } else {
        return false;
    }
}

/**
 *  判断是否为空  null empty  blank undefind
 *  多个参数
 * @param object
 */
export function isVoidArg() {
    var n = arguments.length;
    var t = true;
    for (var i = 0; i < arguments.length; i++) {
        if (isNotVoid(arguments[i])) {
            t = false;
            break
        }
    }
    return t;
}

/**
 *  判断是否为空  null empty  blank undefind
 * @param object
 */
export function isNotVoid(param) {
    return !isVoid(param);
}

/**
 *  判断是否为空  null empty  blank undefind
 *  多个参数
 * @param object
 */
export function isNotVoidArg() {
    var n = arguments.length;
    var t = true;
    for (var i = 0; i < arguments.length; i++) {
        if (isVoid(arguments[i])) {
            t = false;
            break
        }
    }
    return t;
}


/**
 *  判断ab  a b Annualized  是否相等  年化系数
 * @param object
 */
export function isEquativesAnnualizedABC(mnotional, mactualPecentage, mannualized, mpaymentAmount,) {
    if (isEqualNum(mactualPecentage, div(mpaymentAmount, mul(mannualized, mnotional, 10), 10)) ||
        isEqualNum(mnotional, div(mpaymentAmount, mul(mannualized, mactualPecentage, 10), 10)) ||
        isEqualNum(mnotional, div(div(mpaymentAmount, mactualPecentage, 10), mannualized,10)) ||
        isEqualNum(mpaymentAmount, mul(mnotional, mul(mannualized, mactualPecentage, 10),10)) ||
        isEqualNum(mpaymentAmount, mul(mactualPecentage, mul(mannualized, mnotional, 10),10))) {
        return true;
    } else {
        return false;
    }
}

/**
 *  判断ab  a b  是否相等
 * @param object
 */
export function isEquativesABC(ma, mb, mab) {

    if (_.isEqual(round(mb, 10), round(_.divide(mab, ma), 10)) ||
        _.isEqual(round(ma, 10), round(_.divide(mab, mb), 10)) ||
        _.isEqual(round(mab, 10), round(_.multiply(mb, ma), 10))) {
        return true;
    } else {
        return false;
    }

}

/**
 *  乘 多个参数
 * */
export function mulArg(...nums) {
    let num = 1;
    nums.forEach((item) => {
        let mitem = _.toNumber(item);
        num *= mitem
    })
    return _.round(num, 2);
}

/**
 * 相等
 * */
export function isEqualNum(m, n) {
    if (round(m,10) == round(n,10)) {
        return true;
    } else {
        return false;
    }
}

/**
 * 乘
 * */
export function mul(m, n, p) {
    if (p == undefined) {
        p = 2
    }
    return _.round(_.multiply(m, n), p);
}

/**
 * 除
 * */
export function div(m, n, p) {
    if (p == undefined) {
        p = 2
    }
    return _.round(_.divide(m, n), p);
}

/**
 * 加
 * */
export function add(m, n, p) {
    if (p == undefined) {
        p = 2
    }
    return _.round(_.add(m, n), p);
}

/**
 * 减
 * */
export function sub(m, n, p) {
    if (p == undefined) {
        p = 2
    }
    return _.round(_.subtract(m, n), p);
}

/**
 * 四舍五入 精度
 * */
export function round(n, p) {
    if (p == undefined) {
        p = 2
    }
    return _.round(_.toNumber(n), p);
}
export function zhDigitToArabic(digit) {
    const zh = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    const unit = ['千', '百', '十'];
    const quot = ['万', '亿', '兆', '京', '垓', '秭', '穰', '沟', '涧', '正', '载', '极', '恒河沙', '阿僧祗', '那由他', '不可思议', '无量', '大数'];
    let result = 0, quotFlag;

    for (let i = digit.length - 1; i >= 0; i--) {
        if (zh.indexOf(digit[i]) > -1) { // 数字
            if (quotFlag) {
                result += quotFlag * getNumber(digit[i]);
            } else {
                result += getNumber(digit[i]);
            }
        } else if (unit.indexOf(digit[i]) > -1) { // 十分位
            if (quotFlag) {
                result += quotFlag * getUnit(digit[i]) * getNumber(digit[i - 1]);
            } else {
                result += getUnit(digit[i]) * getNumber(digit[i - 1]);
            }
            --i;
        } else if (quot.indexOf(digit[i]) > -1) { // 万分位
            if (unit.indexOf(digit[i - 1]) > -1) {
                if (getNumber(digit[i - 1])) {
                    result += getQuot(digit[i]) * getNumber(digit[i - 1]);
                } else {
                    result += getQuot(digit[i]) * getUnit(digit[i - 1]) * getNumber(digit[i - 2]);
                    quotFlag = getQuot(digit[i]);
                    --i;
                }
            } else {
                result += getQuot(digit[i]) * getNumber(digit[i - 1]);
                quotFlag = getQuot(digit[i]);
            }
            --i;
        }
    }

    return result;

    // 返回中文大写数字对应的阿拉伯数字
    function getNumber(num) {
        for (let i = 0; i < zh.length; i++) {
            if (zh[i] == num) {
                return i;
            }
        }
    }

    // 取单位
    function getUnit(num) {
        for (let i = unit.length; i > 0; i--) {
            if (num == unit[i - 1]) {
                return Math.pow(10, 4 - i);
            }
        }
    }

    // 取分段
    function getQuot(q) {
        for (var i = 0; i < quot.length; i++) {
            if (q == quot[i]) {
                return Math.pow(10, (i + 1) * 4);
            }
        }
    }
}



